const App = getApp()
Page({
  data: {
    list: ''
  },
  onLoad: function (options) {

  },
  onReady: function () {

  },
  onShow: function () {
    let that = this
    that.list()
  },

  // 获取列表
  async list() {
    let that = this
    await App.api.$http({
      url: 'Address/lists',
      method: 'POST',
      data: {}
    }, {}).then(res => {
      if (res) {
        that.setData({
          list: res.data.list
        })
      }
    })
  },
  add() {
    let that = this
    wx.chooseLocation({
      success: function (res) {
        App.api.$http({
          url: 'address/add',
          method: 'POST',
          data: {
            address: res.address,
            latitude: res.latitude,
            longitude: res.longitude
          }
        }, {}).then(res => {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 2000
          })
          that.list()
        })
      },
      fail() {
      }
    })
  },
  // 长按删除
  longPress(e) {
    let that = this
    let id = e.currentTarget.dataset.item.address_id
    wx.showModal({
      title: '提示',
      content: '确定删除吗',
      success(res) {
        if (res.confirm) {
          App.api.$http({
            url: 'address/delete',
            method: 'POST',
            data: {
              address_id: id
            }
          }, {}).then(res => {
            wx.showToast({
              title: res.msg,
              icon: 'none',
              duration: 2000
            })
            that.list()
          })
        } else if (res.cancel) {}
      }
    })
  },

  editaddress(e) {
    let item = e.currentTarget.dataset.item
    wx.setStorageSync('address', item)
    wx.navigateBack()
  },
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})